package com.hotelsystem.reservation.controllers;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import com.hotelsystem.reservations.layouts.OpenUserView;
import com.hotelsystem.reservations.layouts.UserListView;
import com.hotelsystem.reservations.main.MainMenuController;
import com.hotelsystem.reservations.main.MainWindow;
import com.hotelsystem.reservations.persons.Client;
import com.hotelsystem.reservations.persons.ClientManagement;

public class UserListController {

	private UserListView userListView;
	private ArrayList<Client> clients = new ArrayList<>();
	private ClientManagement clientManagement;
	private MainWindow mWindow;

	public UserListController(MainMenuController mainMenuController) {
		userListView = new UserListView();
		clientManagement = new ClientManagement();
		clients = clientManagement.getClients();
		mWindow = mainMenuController.getMainWindow();
		addListenerOpen();
		addListenerDelete();
		addListenerOnList();
	}

	public void fillList() {
		JList<String> clientsList = userListView.getClientsList();
		DefaultListModel<String> clientModels = new DefaultListModel<>();

		for (Client client : clients) {
			clientModels.addElement(client.getFirstName() + " "
					+ client.getLastName());
		}
		clientsList.setModel(clientModels);
	}

	public UserListView getUserListView() {
		return userListView;
	}

	public void addListenerOpen() {
		userListView.addListenerForOpen(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {

				clickButtonOpen();
				
				int clientIndex = userListView.getClientsList()
						.getSelectedIndex();
				Client client = clients.get(clientIndex);
				OpenUserView openUser = new OpenUserView(client, false);
                //System.out.println(client.toString());
				// MainWindow mWindow = new MainWindow();
			//	mWindow.getMainFrame().repaint();
				mWindow.setJPanel(openUser.getMainPanel());
		
			}

		});
	}

	private void clickButtonOpen() {
		int clientIndex = userListView.getClientsList().getSelectedIndex();
		Client client = clients.get(clientIndex);
		OpenUserView openUser = new OpenUserView(client, false);
		// MainWindow mWindow = new MainWindow();
		// mWindow.getMainFrame().repaint();
		mWindow.setJPanel(openUser.getMainPanel());
	}

	public void addListenerOnList() {
		userListView.addMouseListener(new MouseListener() {

			@Override
			public void mouseReleased(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseClicked(MouseEvent evt) {
				if (evt.getClickCount() == 2) {
					clickButtonOpen();
				}
			}
		});
	}

	public void addListenerDelete() {
		userListView.addListenerForDelete(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				int clientIndex = userListView.getClientsList()
						.getSelectedIndex();

				JList<String> jList = userListView.getClientsList();
				((DefaultListModel<String>) jList.getModel())
						.remove(clientIndex);
				Client client = clients.get(clientIndex);
				clientManagement.deleteClient(client);
				// mWindow.getMainFrame(). getContentPane().removeAll();
				mWindow.getMainFrame().getContentPane().invalidate();
				mWindow.getMainFrame().getContentPane().validate();
				// mWindow.setJPanel(openUser.getPanel());
			}
		});
	}

	public JPanel getUserListPanel() {
		return userListView.getMainPanel();
	}
	// public static void main(String[] args) {
	// UserListController controller = new UserListController();
	// JFrame frame = new JFrame();
	// JPanel panel = controller.getUserListView().getMainPanel();
	// frame.add(panel);
	// frame.setSize(400, 200);
	// frame.setVisible(true);
	// controller.fillList();
	// }
}
